package com.hongyun.tms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hongyun.tms.common.constants.ConstantsTypeEnum;
import com.hongyun.tms.common.dto.tug.SelecSearchDTO;
import com.hongyun.tms.common.enums.DictTypeEnum;
import com.hongyun.tms.common.enums.SelectEnum;
import com.hongyun.tms.common.utils.StringUtil;
import com.hongyun.tms.mapper.SearchMapper;
import com.hongyun.tms.service.*;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * 下拉框数据源
 */
@Service
public class SearchServiceImpl implements SearchService {


    @Resource
    public SearchMapper searchMapper;

    @Resource
    public IMWorkItemService workItemService;

    @Resource
    public TmsMTugService tugService;

    @Resource
    public IMShipService shipService;
    @Resource
    public IMCustomerService customerService;

    /**
     * 宽泛船舶状态枚举
     * <p>
     * 预到 包含：接收
     * 锚地 包含：抵锚、起锚
     * 在港 包含：靠泊、移泊、开工、停工、复工、完工
     * 离泊 包含：离泊、离港
     * <p>
     * *不包含作废状态*
     * *不包含船舶动态之前的状态：预报*
     * *移泊是“操作”而不是“状态”，也不包含*
     */
    @Getter
    private enum ShipStatusBroadEnum {

        YUDAO("00", "预到"),

        MAODI("10", "锚地"),

        ZAIGANG("20", "在港"),

        LIBO("30", "离泊");

        private final String code;

        private final String name;

        ShipStatusBroadEnum(String code, String comment) {
            this.code = code;
            this.name = comment;
        }

        public static boolean isContains(String code) {
            return Arrays.stream(SearchServiceImpl.ShipStatusBroadEnum.values()).anyMatch(v1 -> v1.getCode().equals(code));
        }
    }

    @Override
    public List<Map<String, Object>> getRemoteSelect(SelecSearchDTO selectCommonSearch) {
        // 搜索值和选中值都为空时 直接返回
        if ((selectCommonSearch.getValueContent() == null || "".equals(selectCommonSearch.getValueContent()))
                && (selectCommonSearch.getLabelContent() == null || "".equals(selectCommonSearch.getLabelContent()))) {
            return new ArrayList<>();
        }
        if (selectCommonSearch.getValueContent() == null || "".equals(selectCommonSearch.getValueContent())) {
            selectCommonSearch.setValueContentList(new ArrayList<>());
        } else {
            // 多选时兼容逗号分隔的多个key
            selectCommonSearch.setValueContentList(Arrays.asList(selectCommonSearch.getValueContent().split(",")));
        }
        List<Map<String, Object>> res = new ArrayList<>();
        switch (selectCommonSearch.getType()) {
            // 人员信息
            case "STAFF_INFO":
                res = searchMapper.getStaffInfoList(selectCommonSearch);
                break;

            // 作业项目
            case "WORK_ITEM":
                res = workItemService.getWorkItemList(selectCommonSearch);
                break;

            //拖轮信息
            case "TUG_INFO":
                res = tugService.getTugList(selectCommonSearch);
                break;

            //船舶信息
            case "SHIP_INFO":
                res = shipService.getShipList(selectCommonSearch);
                break;

            //客户信息
            case "CUSTOMER_INFO":
                res = customerService.getCustomerList(selectCommonSearch);
                break;
            default:
                break;
        }

        return res;

    }

    /**
     * 获取本地下拉框数据源
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getLocalSelect(Map<String, Object> params) {

        // 类型
        String type = StrUtil.toString(params.get("type"));
        // 父节点
        Object parent = params.get("parent");

        List<Map<String, Object>> res = new ArrayList<>();

        switch (type) {
            // 作业公司
            case "DEPT_WORK_COMPANY":
                res = searchMapper.getLocalSelect(
                        SelectEnum.DEPT_WORK_COMPANY.getTableName(),
                        SelectEnum.DEPT_WORK_COMPANY.getValueName(),
                        SelectEnum.DEPT_WORK_COMPANY.getLabelName(),
                        " IS_WORK_COMPANY = '1' AND STATE = '0'");
                break;

            // 泊位
            case "BERTH":
                res = searchMapper.getLocalSelect(
                        SelectEnum.BERTH.getTableName(),
                        SelectEnum.BERTH.getValueName(),
                        SelectEnum.BERTH.getLabelName(),
                        " STATUS = '1' ");
                break;
            // 泊位缆庄
            case "BERTH_BOLLARD":
                if (StringUtil.isEmpty(StringUtil.getString(parent))) {
                    return new ArrayList<>();
                } else {
                    res = searchMapper.getLocalSelect(
                            SelectEnum.BERTH_BOLLARD.getTableName(),
                            SelectEnum.BERTH_BOLLARD.getValueName(),
                            SelectEnum.BERTH_BOLLARD.getLabelName(),
                            " BERTH_ID = " + (StringUtils.isEmpty(StringUtil.getString(parent)) ? StringUtils.EMPTY : Long.parseLong(StringUtil.getString(parent))));
                }
                break;
            // 拖轮
            case "TUG":
                res = searchMapper.getLocalSelect(
                        SelectEnum.TUG.getTableName(),
                        SelectEnum.TUG.getValueName(),
                        SelectEnum.TUG.getLabelName(),
                        null);
                break;
            // 字典
            case "DICT":
                String dictType = StringUtil.getString(params.get("dictType"));
                res = searchMapper.getLocalSelect(
                        SelectEnum.DICT.getTableName(),
                        SelectEnum.DICT.getValueName(),
                        SelectEnum.DICT.getLabelName(),
                        " DICT_TYPE = '" + DictTypeEnum.match(dictType) + "' AND STATUS = '1' ");
                break;
            case "CONSTANT":
                String constantType = StringUtil.getString(params.get("types"));
                res = searchMapper.getLocalSelect(
                        SelectEnum.CONSTANT.getTableName(),
                        SelectEnum.CONSTANT.getValueName(),
                        SelectEnum.CONSTANT.getLabelName(),
                        " TYPE_CD = '" + ConstantsTypeEnum.match(constantType) + "'");
                break;
            case "STORAGE_YARD_LEVEL":
                if (StringUtil.isEmpty(StringUtil.getString(parent))) {
                    return new ArrayList<>();
                } else {
                    String level = StringUtil.getString(params.get("level"));
                    level = StringUtils.isEmpty(level) ? StringUtils.EMPTY : String.valueOf(Integer.parseInt(level));

                    res = searchMapper.getLocalSelect(
                            SelectEnum.STORAGE_YARD_LEVEL.getTableName(),
                            SelectEnum.STORAGE_YARD_LEVEL.getValueName(),
                            SelectEnum.STORAGE_YARD_LEVEL.getLabelName(),
                            " PARENT_ID = " + Long.parseLong(String.valueOf(parent)) + " AND STORAGE_YARD_LEVEL = '" + level + "'");
                }
                break;
            case "STANDARD_SYSTEM":
                res = searchMapper.getLocalSelect(
                        SelectEnum.STANDARD_SYSTEM.getTableName(),
                        SelectEnum.STANDARD_SYSTEM.getValueName(),
                        SelectEnum.STANDARD_SYSTEM.getLabelName(), null);
                break;
            //工艺流程
            case "PROCESS":
                res = searchMapper.getLocalSelect(
                        SelectEnum.PROCESS.getTableName(),
                        SelectEnum.PROCESS.getValueName(),
                        SelectEnum.PROCESS.getLabelName(), null);
                break;
            //行名航次
            case "TOS_DIS_SHIPVOYAGE":
                res = searchMapper.getLocalSelect(
                        SelectEnum.TOS_DIS_SHIPVOYAGE.getTableName(),
                        SelectEnum.TOS_DIS_SHIPVOYAGE.getValueName(),
                        SelectEnum.TOS_DIS_SHIPVOYAGE.getLabelName(), null);
                break;

            default:
                break;
        }

        return res;

    }

    /**
     * 获取本地下拉框数据源
     *
     * @param types
     * @return
     */
    @Override
    public HashMap<String, List<Map<String, Object>>> getLocalSelects(String types) {

        String[] arrayTypes = types.split(",");

        HashMap<String, List<Map<String, Object>>> map = new HashMap<>();

        // 循环查询多个本地下拉框
        for (String type : arrayTypes) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("type", type);
            map.put(type, this.getLocalSelect(params));
        }

        return map;
    }
}
